--[[/**
-- * 
* 抽取敌方血量,治疗单位（1=自己，2=自己+生命最低的友军）
 * 
-- *
-- */]]

---@class BuffEffect466101 : BaseBuffEffect
BuffEffect466101 = ClientFight.CreateClass("BuffEffect466101", ClientFight.BaseBuffEffect)
local t = BuffEffect466101

---@param fight Fight
---@param buff FighterBuff
function t:action(fight, buff, ...)
    local parameters = {...};
    local buffEffectValue = buff.buffBean.f_BuffEffectValue;
    local target = parameters[1];
    local maxHp = FighterAttributeManager.getFightAttr(target, FighterAttributeEnum.HP);
    local addHp = Utils.calcRatio(maxHp, buffEffectValue[1] * buff.overlay);

    local fightAttr = FighterAttributeManager.getFightAttr(target, FighterAttributeEnum.ATK);
    if (addHp > fightAttr * buff.overlay) then
        --2021-12-16 临时修bug, 策划说先写死
        addHp = fightAttr * buff.overlay;
    end
    local logAddHp = addHp;
    SkillRoleMgr.savePathFile("[" .. fight.frame .. "][EFFECT]Fighter " .. buff.target.fighterId .. " Buff"
            .. buff.modelId .. " 偷取敌方血量: " .. logAddHp);

    --扣除敌人的血量
    DamageManager.setFighterHp(fight, target, buff.target, nil, DamageResultTypeEnum.NORMAL,
            -addHp, target.hp - addHp, false, true, true);
    --给自己奶一口
    DamageManager.setFighterHp(fight, buff.target, buff.target, nil, DamageResultTypeEnum.HEAL,
            addHp, buff.target.hp + addHp, true, false, true);
    --如果是升级技能

    if (buffEffectValue.length > 1) then
        --寻找除了自己之外的 血量最低的队友,奶一口
        local fighters = fight.teams[buff.target.teamIndex].fighters;
        local minHp = nil;
        for k, fighter in paris(fighters) do
            if (fighter == buff.target) then
                goto continue;
            end
            if (fighter:isSummons()) then
                goto continue;
            end
            if (fighter:isDie()) then
                goto continue;
            end
            if (minHp == nil) then
                minHp = fighter;
                goto continue;
            end
            if (fighter.hp < minHp.hp) then
                minHp = fighter;
            end
            ::continue::
        end
        if (minHp ~= nil) then
            --给自己奶一口
            DamageManager.setFighterHp(fight, minHp, buff.target, nil, DamageResultTypeEnum.HEAL,
                    addHp, minHp.hp + addHp, true, false, true);
        end
    end
    return ClientFight.BaseBuffEffect.action(self, fight, buff, ...);
end

t.New()